package com.xq.interceptor;


import com.alibaba.fastjson.JSON;
import com.xq.base.ResponseResult;
import com.xq.exception.BizException;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@Log4j2
public class GlobalExceptionHandler {


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exception(HttpServletRequest req, Exception e) {
        if (e instanceof BizException) {
            BizException biz = (BizException) e;
            log.info("错误结果:"+ JSON.toJSONString(ResponseResult.error(biz.getCode(), biz.getMessage())));
            return ResponseResult.error(biz.getCode(), biz.getMessage());
        } else {
            log.error("系统异常", e);
            return ResponseResult.error(e.getMessage());
        }
    }

}

